﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class ReadAndroid : MonoBehaviour
{
    public Button OpenGalleryBut;
    public Button OpenCameraBut;
    public Button AndroidShockBut;
    public RawImage RawImg;
    public Image SpriteImg;
    private AndroidJavaObject m_Ajc;
    private AndroidJavaClass m_Ac;


    // Start is called before the first frame update
    void Start()
    {
        OpenCameraBut.onClick.AddListener(() =>
        {
            OpenCameraButClick();
        });

        OpenGalleryBut.onClick.AddListener(()=> {
            OpenGalleryButClick();
        });

        AndroidShockBut.onClick.AddListener(() =>
        {
            AndroidShockButClick();
        });

  
        m_Ac = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        m_Ajc = m_Ac.GetStatic<AndroidJavaObject>("currentActivity");
        if(m_Ajc==null)
        {
            Debug.Log("Ajc == null");
        }
        else
        {
            Debug.Log("Ajc Have Value");
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OpenCameraButClick()
    {
        m_Ajc.Call("OpenCamera");
    }


    private void OpenGalleryButClick()
    {
        m_Ajc.Call("OpenGallery");
    }

    private void AndroidShockButClick()
    {
        m_Ajc.Call("Vibrator",200);
    }

    private void FileGetTex(string path)
    {
        FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
        fileStream.Seek(0, SeekOrigin.Begin);
        byte[] bye = new byte[fileStream.Length];
        Debug.Log("Byte:" + bye.Length);
        fileStream.Read(bye, 0, bye.Length);
        fileStream.Close();

        Texture2D texture2D = new Texture2D((int)RawImg.rectTransform.rect.width, (int)RawImg.rectTransform.rect.height);
        texture2D.LoadImage(bye);
        if (texture2D==null)
        {
            Debug.Log("Texture为null");
        }
        RawImg.texture = texture2D;
        StartCoroutine(UpdateImage(texture2D));
    }

    /// <summary>
    /// Android裁剪完图片进行回调
    /// </summary>
    /// <param name="imageName"></param>
    public void OnImagePath(string imagePath)
    {
        if(imagePath.Contains("Fail"))
        {
            return;
        }

        StartCoroutine(LoadImage(imagePath));
    }

    private IEnumerator LoadImage(string imagePath)
    {
        Debug.Log("Unity:" + imagePath);
        WWW www = new WWW("file://" + imagePath);
        yield return www;
        if(www.error==null)
        {
            StartCoroutine(UpdateImage(www.texture));
        }
    }

    //纹理转化为精灵
    private IEnumerator UpdateImage(Texture2D texture)
    {
        Debug.Log("Texture Width:" + texture.width + ";Texture Height:" + texture.height);
        Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
        SpriteImg.sprite = sprite;
        yield return new WaitForSeconds(0.01f);
        Resources.UnloadUnusedAssets();
    }

    //Android调用获取图片地址
    public void GetPhoto(string path)
    {
        Debug.Log("android give path ==> " + path);
        //StartCoroutine(DownloadTexture(path,RawImg));
        FileGetTex(path);
    }

}
